package com.ebrothers.isns.controller;

import java.util.ArrayList;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ebrothers.isns.R;
import com.ebrothers.isns.common.Constants;
import com.ebrothers.isns.data.QsInteractor;
import com.ebrothers.isns.vo.Answer;
import com.ebrothers.isns.vo.Question;

public class AsViewController extends ViewController {
	private static final String TAG = "AsViewController";
	private ArrayList<Answer> mAnswers;
	private TextView mQLocation;
	private TextView mQ;
	private TextView mAnswerCount;
	private AsListAdapter mAdapter;

	public AsViewController(Context context, View view) {
		super(context, view);
	}

	@Override
	public void onViewBinded(View view) {
		mAnswers = new ArrayList<Answer>();
		mQLocation = (TextView) view.findViewById(R.id.owner_location);
		mQ = (TextView) view.findViewById(R.id.question);
		mAnswerCount = (TextView) view.findViewById(R.id.answer_count);
		ListView answers = (ListView) view.findViewById(R.id.answers);
		AnimationSet set = new AnimationSet(true);

		Animation animation = new AlphaAnimation(0.0f, 1.0f);
		animation.setDuration(50);
		set.addAnimation(animation);

		animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		animation.setDuration(100);
		set.addAnimation(animation);

		LayoutAnimationController controller = new LayoutAnimationController(
				set, 0.5f);
		answers.setLayoutAnimation(controller);
		mAdapter = new AsListAdapter();
		answers.setAdapter(mAdapter);
	}

	@Override
	public void onViewSelected(Bundle data) {
		mAnswers.clear();
		mAdapter.notifyDataSetChanged();
		Question q = (Question) data.get("q");
		mQLocation.setText(q.triggerLocation.name);
		mQ.setText(q.body);
		new AsAsyncTask().execute(q.id);
	}

	class AsAsyncTask extends AsyncTask<Long, Void, ArrayList<Answer>> {
		@Override
		protected ArrayList<Answer> doInBackground(Long... params) {
			return QsInteractor.getAnswers(params[0]);
		}

		@Override
		protected void onPostExecute(ArrayList<Answer> result) {
			if (Constants.LOGD) {
				Log.d(TAG, "****** onPostExecute. result=" + result.size());
			}
			mAnswers.addAll(result);
			mAdapter.notifyDataSetChanged();
			int count = result.size();
			String label = " answers";
			if (count <= 1) {
				label = " answer";
			}
			mAnswerCount.setText(count + label);
		}
	}

	class AsListAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public AsListAdapter() {
			mInflater = LayoutInflater.from(getContext());
		}

		public int getCount() {
			return mAnswers.size();
		}

		public Object getItem(int position) {
			return mAnswers.get(position);
		}

		public long getItemId(int position) {
			return mAnswers.get(position).id;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			AsViewHolder holder = null;
			if (convertView == null) {
				convertView = mInflater
						.inflate(R.layout.isns_answer_item, null);
				holder = new AsViewHolder();
				holder.ownerHead = (ImageView) convertView
						.findViewById(R.id.owner_head);
				holder.body = (TextView) convertView
						.findViewById(R.id.answer_body);
				convertView.setTag(holder);
			} else {
				holder = (AsViewHolder) convertView.getTag();
			}

			final Answer a = mAnswers.get(position);
			holder.body.setText(a.body);
			return convertView;
		}
	}

	class AsViewHolder {
		ImageView ownerHead;
		TextView body;
	}

}
